<template>
  <div style="margin: 20px; margin-top: 60px">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="auto"
      >




        <el-form-item :label="t('searchbar.type')" prop="type">
          <el-select
            v-model="queryParams.type"
            :placeholder="t('searchbar.typeInfo')"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.YQ_TYPE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>



        <el-form-item :label="t('searchbar.model')" prop="mode">
          <el-select
            v-model="queryParams.mode"
            filterable
            :placeholder="t('searchbar.modelInfo')"
            clearable
            class="!w-240px"
            :remote-method="remoteMethod"
            :loading="stuAllLoading"
            :filter-method="handleChange"
          >
            <el-option
              v-for="item in deviceList"
              :key="item.id"
              :label="item.type"
              :value="item.id"
              @click="addForm(item)"
            >
              <span style="float: left">{{ item.mode }}({{ t('searchbar.deviceModeCode') }}:{{ item.deviceModeCode }})</span>
            </el-option>
          </el-select>
        </el-form-item>

<!--        <el-form-item :label="t('searchbar.model')" prop="mode">
          <el-input
            v-model="queryParams.mode"
            :placeholder="t('searchbar.modelInfo')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>-->

        <el-form-item :label="t('searchbar.deviceModeCode')" prop="deviceModeCode">
          <el-input
            v-model="queryParams.deviceModeCode"
            :placeholder="t('searchbar.deviceModeCodeInfo')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <!--      <el-form-item :label="t('searchbar.model')" prop="mode">
                <el-select
                  v-model="queryParams.mode"
                  :placeholder="t('searchbar.modelInfo')"
                  v-if="getSelectOption"
                  clearable
                  class="!w-240px"
                >
                  <el-option
                    v-for="dict in getSelectOption"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>-->

        <!--      <el-form-item :label="t('searchbar.createTime')" prop="createTime">
                <el-date-picker
                  v-model="queryParams.createTime"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  type="datetimerange"
                  :start-placeholder="t('searchbar.startDate')"
                  :end-placeholder="t('searchbar.endDate')"
                  :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
                  class="!w-240px"
                />
              </el-form-item>-->


        <el-form-item>
          <el-button @click="handleQuery">
            <Icon icon="ep:search" class="mr-5px" /> {{ t('action.search') }}
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px" /> {{ t('action.reset') }}
          </el-button>

<!--          <el-button
            type="primary"
            plain
            @click="openForm('create')"
            v-hasPermi="['base:site:create']"
          >
            <Icon icon="ep:plus" class="mr-5px" /> {{ t('action.create') }}
          </el-button>-->

<!--          <el-button-->
<!--            type="warning"-->
<!--            plain-->
<!--            @click="handleImport"-->
<!--          >-->
<!--            <Icon icon="ep:upload" /> {{ t('action.import') }}-->
<!--          </el-button>-->

<!--          <el-button-->
<!--            type="success"-->
<!--            plain-->
<!--            @click="handleExportDevicebase"-->
<!--            :loading="exportLoading"-->
<!--            v-hasPermi="['base:site:export']"-->
<!--          >-->
<!--            <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}-->
<!--          </el-button>-->

        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column :label="t('searchbar.deviceModeCode')" align="center" prop="deviceModeCode" />

        <el-table-column :label="t('formData.type')" align="center" prop="type">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.YQ_TYPE" :value="scope.row.type" />
          </template>
        </el-table-column>
        <el-table-column :label="t('formData.factoryCode')" align="center" prop="factoryCode">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.FACTORY_CODE" :value="scope.row.factoryCode" />
          </template>
        </el-table-column>
        <el-table-column :label="t('formData.model')" align="center" prop="mode" />


        <!--      <el-table-column :label="t('formData.model')" align="center" prop="mode">
                <template #default="scope">
                  <dict-tag :type="getDictType(scope.row.type)" :value="scope.row.mode" />
                </template>
              </el-table-column>-->

        <el-table-column :label="t('formItems.protocol')" align="center" prop="protocol" >
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.XY_TYPE" :value="scope.row.protocol" />
          </template>
        </el-table-column>
        <el-table-column :label="t('formItems.port')" align="center" prop="port" />
        <el-table-column :label="t('formItems.ipAddress')" align="center" prop="ip" />

        <el-table-column :label="t('formData.remark')" align="center" prop="remark" />
        <el-table-column :label="t('formData.state')" align="center" prop="state">
          <template #default="scope">
            <el-tag :type="scope.row.state ? 'success' : 'danger'">
              {{ scope.row.state ? t('action.abled') : t('action.disabled') }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          :label="t('formData.createTime')"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          width="180px"
        />

        <!--      <el-table-column
                :label="t('formData.operate')"
                align="center"
                fixed="right"
                width="180px"
              >
                <template #default="scope">
                  <el-button
                    link
                    type="primary"
                    @click="openForm('update', scope.row.id)"
                    v-hasPermi="['base:device-list:update']"
                  >
                    {{ t('action.edit') }}
                  </el-button>
                  <el-button
                    link
                    type="danger"
                    @click="handleDelete(scope.row.id)"
                    v-hasPermi="['base:device-list:delete']"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>-->
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </div>
  <!-- 表单弹窗：添加/修改 -->
  <DeviceListForm ref="formRef" @success="getList" />
  <!-- 对象导入模态框 -->
  <ImportForm ref="importFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { DeviceBaseApi, DeviceBaseVO } from '@/api/base/devicebase'
import DeviceListForm from './DeviceBaseForm.vue'
import ImportForm from "@/views/base/devicebase/ImportForm.vue";
/** 仪器型号查看 列表 */
defineOptions({ name: 'DeviceBase' })


const stuAllLoading = ref(false)


const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DeviceBaseVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  deviceModeCode: undefined,
  type: undefined,
  mode: undefined,
  createTime: [],
  protocol: undefined,
  port: undefined,
  ip: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DeviceBaseApi.getDeviceBasePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DeviceBaseApi.deleteDeviceBase(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExportDevicebase = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DeviceBaseApi.exportDeviceBase(queryParams)
    download.excel(data, 'devicebase.xlsx')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})

/*

/!*-------------------仪器与型号表单关联部分----------------------*!/
const getDictType = (type) => {
  const dictTypes = {
    A: DICT_TYPE.A,
    B: DICT_TYPE.B,
    C: DICT_TYPE.C,
    D: DICT_TYPE.D,
    E: DICT_TYPE.E,
    F: DICT_TYPE.F,
    G: DICT_TYPE.G,
    H: DICT_TYPE.H
  }
  return dictTypes[type] || '' // 返回对应的字典类型，如果不存在则返回空字符串
}
/!*-------------------仪器与型号搜索框关联部分----------------------*!/
// 计算属性，根据 queryParams.type 返回相应的选项列表
const getSelectOption = computed(() => {
  const dictTypes = {
    A: DICT_TYPE.A,
    B: DICT_TYPE.B,
    C: DICT_TYPE.C,
    D: DICT_TYPE.D,
    E: DICT_TYPE.E,
    F: DICT_TYPE.F,
    G: DICT_TYPE.G,
    H: DICT_TYPE.H
  }
  return getStrDictOptions(dictTypes[queryParams.type])
})
*/



/*---------------------------获取仪器型号清单信息------------------------------*/

const deviceList = ref<DeviceBaseVO[]>([])
const deviceParams = reactive({
  type: undefined,
  mode: undefined,
})
const remoteMethod = async (query) => {
  stuAllLoading.value = true
  modeList.value = []
  stuAllLoading.value = false


  deviceParams.type = queryParams.type
  deviceParams.mode = query
  deviceList.value= await DeviceBaseApi.getDeviceList(deviceParams)
}
const handleChange = async (query) => {
  deviceList.value=[]

  deviceParams.type = queryParams.type
  deviceParams.mode = query
  deviceList.value = await DeviceBaseApi.getDeviceList(deviceParams)

  queryParams.mode = item.value

}
const addForm = (item) => {
  // queryParams.type = item.type
  queryParams.mode = item.mode
  queryParams.deviceModeCode = item.deviceModeCode
}
//-----------------------------------------------------------------

/*------------------------------导入部分-----------------------------------*/
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}


</script>
